{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "44d93b81",
   "metadata": {},
   "outputs": [],
   "source": [
    "from gs_quant.backtests.triggers import DateTriggerRequirements, DateTrigger\n",
    "from gs_quant.backtests.actions import AddTradeAction\n",
    "from gs_quant.backtests.core import ValuationFixingType\n",
    "from gs_quant.backtests.strategy import Strategy\n",
    "from gs_quant.backtests.predefined_asset_engine import PredefinedAssetEngine\n",
    "from gs_quant.backtests.data_sources import DataManager\n",
    "from gs_quant.instrument import FXOption\n",
    "from gs_quant.data import DataFrequency\n",
    "\n",
    "from random import random\n",
    "import pandas as pd\n",
    "from datetime import date, datetime, timedelta\n",
    "\n",
    "from gs_quant.session import GsSession\n",
    "GsSession.use(client_id=None, client_secret=None, scopes=('run_analytics',))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2b213057",
   "metadata": {},
   "outputs": [],
   "source": [
    "trigger_times = [datetime(2021, 1, 4, 14, 43, 0), \n",
    "                 datetime(2021, 1, 6, 14, 43, 0), \n",
    "                 datetime(2021, 1, 6, 14, 58, 0), \n",
    "                 datetime(2021, 1, 7, 13, 28, 0), \n",
    "                 datetime(2021, 1, 7, 14, 58, 0), \n",
    "                 datetime(2021, 1, 8, 13, 28, 0), \n",
    "                 datetime(2021, 1, 13, 13, 28, 0), \n",
    "                 datetime(2021, 1, 14, 13, 28, 0),\n",
    "                 datetime(2021, 1, 15, 13, 28, 0),\n",
    "                 datetime(2021, 1, 15, 14, 13, 0),\n",
    "                 datetime(2021, 1, 15, 14, 58, 0),\n",
    "                 datetime(2021, 1, 21, 13, 28, 0),\n",
    "                 datetime(2021, 1, 22, 14, 43, 0),\n",
    "                 datetime(2021, 1, 22, 14, 58, 0),\n",
    "                 datetime(2021, 1, 26, 14, 58, 0),\n",
    "                 datetime(2021, 1, 27, 13, 28, 0),\n",
    "                 datetime(2021, 1, 27, 18, 58, 0),\n",
    "                 datetime(2021, 1, 28, 13, 28, 0),\n",
    "                 datetime(2021, 1, 28, 14, 58, 0),\n",
    "                 datetime(2021, 1, 29, 13, 28, 0),\n",
    "                 datetime(2021, 1, 29, 14, 58, 0),\n",
    "                 datetime(2021, 2, 1, 14, 43, 0),\n",
    "                 datetime(2021, 2, 1, 14, 58, 0),\n",
    "                 datetime(2021, 2, 3, 14, 43, 0),\n",
    "                 datetime(2021, 2, 3, 14, 58, 0),\n",
    "                 datetime(2021, 2, 4, 13, 28, 0),\n",
    "                 datetime(2021, 2, 4, 14, 58, 0),\n",
    "                 datetime(2021, 2, 5, 13, 28, 0),\n",
    "                 datetime(2021, 2, 10, 13, 28, 0),\n",
    "                 datetime(2021, 2, 11, 13, 28, 0),\n",
    "                 datetime(2021, 2, 12, 14, 58, 0),\n",
    "                 datetime(2021, 2, 17, 13, 28, 0),\n",
    "                 datetime(2021, 2, 17, 14, 13, 0),\n",
    "                 datetime(2021, 2, 18, 13, 28, 0),\n",
    "                 datetime(2021, 2, 19, 14, 43, 0),\n",
    "                 datetime(2021, 2, 19, 14, 58, 0),\n",
    "                 datetime(2021, 2, 23, 14, 58, 0),\n",
    "                 datetime(2021, 2, 24, 14, 58, 0),\n",
    "                 datetime(2021, 2, 25, 13, 28, 0),\n",
    "                 datetime(2021, 2, 26, 13, 28, 0),\n",
    "                 datetime(2021, 2, 26, 14, 58, 0),\n",
    "                 datetime(2021, 3, 1, 14, 43, 0),\n",
    "                 datetime(2021, 3, 1, 14, 58, 0),\n",
    "                 datetime(2021, 3, 3, 14, 43, 0),\n",
    "                 datetime(2021, 3, 3, 14, 58, 0),\n",
    "                 datetime(2021, 3, 4, 13, 28, 0),\n",
    "                 datetime(2021, 3, 4, 14, 58, 0),\n",
    "                 datetime(2021, 3, 5, 13, 28, 0),\n",
    "                 datetime(2021, 3, 10, 13, 28, 0)]\n",
    "\n",
    "start_date, end_date = min(trigger_times), max(trigger_times)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6e26342c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Asset and Prices of asset\n",
    "# the asset defined here could be anything as we are going to provide the valuation of that asset.\n",
    "asset = FXOption(buy_sell='Buy', option_type='Put', pair='EURUSD',  notional_amount='10m', name='EURUSD')\n",
    "\n",
    "\n",
    "# we need to provide the price of the asset in a data_manager\n",
    "\n",
    "data_manager = DataManager()\n",
    "index = pd.date_range(start_date, end_date.date() + timedelta(days=1), freq='T')\n",
    "# Here I am constructing random prices for the asset\n",
    "price = [100]\n",
    "for i in range(1, len(index)):\n",
    "    price.append(price[i-1] + (-1 if random() < 0.5 else 1))\n",
    "prices = pd.Series(price, index=index)\n",
    "\n",
    "eod_prices = prices.at_time('16:00').dropna()\n",
    "\n",
    "data_manager.add_data_source(prices,\n",
    "                             DataFrequency.REAL_TIME,\n",
    "                             asset,\n",
    "                             ValuationFixingType.PRICE)\n",
    "\n",
    "data_manager.add_data_source(eod_prices,\n",
    "                             DataFrequency.DAILY,\n",
    "                             asset,\n",
    "                             ValuationFixingType.PRICE)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "991db60a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# define the triggers, actions and strategy - this is common across all backtest engine implementations\n",
    "\n",
    "# the DateTrigger takes a list of dates when the an action should be taken\n",
    "trig_req = DateTriggerRequirements(trigger_times)\n",
    "# we are going to add our asset and hold it for 30 minutes\n",
    "trigger = DateTrigger(trig_req, AddTradeAction(asset, pd.Timedelta(minutes=30)))\n",
    "strategy = Strategy(None, trigger)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ddee6af3",
   "metadata": {},
   "outputs": [],
   "source": [
    "# construct a PredefinedAssetEngine including our asset prices and then run the backtest\n",
    "engine = PredefinedAssetEngine(data_manager)\n",
    "bt = engine.run_backtest(strategy, start=start_date, end=end_date, frequency='D')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "468d4f3e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# we can look at the performance of the backtest\n",
    "\n",
    "bt.performance.plot()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "efeaa099",
   "metadata": {},
   "outputs": [],
   "source": [
    "# or we can look at a trade ledge which shows the trades that were entered and their individual pnls\n",
    "bt.trade_ledger()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f77f1e1d",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6f12331a",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
